package com.lianzhihui.romeex.ui.account;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lianzhihui.romeex.bean.user.CountryCode;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.net.APIConstant;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.base.CustomWebActivity;
import com.lianzhihui.romeex.utils.EdittextUtil;
import com.lianzhihui.romeex.utils.MyToast;
import com.lianzhihui.romeex.mvp.view.LoginView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 设置昵称界面
 * Author: RAMON
 * CreateDate: 2020/10/27
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class SetNameSaftyActivity extends BaseActivity implements LoginView {

    @BindView(R.id.et_account_name)
    EditText et_account_name;
    @BindView(R.id.et_account_password)
    EditText et_account_password;
    @BindView(R.id.et_account_confirm)
    EditText et_account_confirm;
    @BindView(R.id.iv_eyes1)
    ImageView iv_eyes1;
    @BindView(R.id.iv_eyes2)
    ImageView iv_eyes2;
    @BindView(R.id.cb_agree)
    CheckBox cb_agree;
    @BindView(R.id.bt_compare)
    Button bt_compare;
    @BindView(R.id.include_top_tv_title)
    TextView include_top_tv_title;

    //是否由管理跳转创建 false为第一次创建
    private boolean isManage;
    private WalletInfo walletInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_name_safty;
    }

    @Override
    protected void initView() {
        include_top_tv_title.setText(getResources().getText(R.string.toolbar_title_safty_code));
        isManage = getIntent().getBooleanExtra("isManage", false);

        et_account_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                chackNext();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_account_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                chackNext();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_account_confirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                chackNext();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    public void chackNext() {
        if (et_account_name.getText().toString().trim().length() > 0) {
            if (et_account_password.getText().toString().trim().length() > 0) {
                if (et_account_confirm.getText().toString().trim().length() > 0) {
                    bt_compare.setEnabled(true);
                    return;
                }
            }
        }
        bt_compare.setEnabled(false);
    }

    @Override
    protected void initData() {

    }

    /**
     * 下一步，跳转设置安全码
     */
    private void setCode() {
        hideInput();
        String account = et_account_name.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(mActivity, getString(R.string.toast_input_account), Toast.LENGTH_SHORT).show();
            return;
        }

        //验证密码
        String password = et_account_password.getText().toString().trim();
        String confirm = et_account_confirm.getText().toString().trim();

        if (TextUtils.isEmpty(password) || password.length() != 6) {
            Toast.makeText(mActivity, getString(R.string.toast_password_wrong), Toast.LENGTH_SHORT).show();
            return;
        }

        if (!password.equals(confirm)) {
            Toast.makeText(mActivity, getString(R.string.toast_password_diff), Toast.LENGTH_SHORT).show();
            return;
        }

        if (!cb_agree.isChecked()) {
            MyToast.showToast("请阅读并同意条款");
            return;
        }

        walletInfo = new WalletInfo();
        walletInfo.setUserName(account);
        walletInfo.setSaftyCodeSample(password);
//        Intent intent = new Intent(this, SetSaftyCodeActivity.class);

        Intent intent = new Intent(this, MnemonicWordActivity.class);
        intent.putExtra("isManage", isManage);
        intent.putExtra("walletInfo", walletInfo);
        startActivity(intent);
        finish();
    }

    @Override
    public void setAccountData(WalletInfo data) {

    }

    @Override
    public void setImportData(WalletInfo data) {

    }

    @Override
    public void setSyncData(boolean success, int code) {
    }

    @Override
    public void setWalletData(WalletInfo data) {
    }

    @Override
    public void setCountryCodeData(List<CountryCode> data) {

    }

    @Override
    public void setPhoneCodeData(String data) {

    }

    @Override
    public void setSyncPhoneCodeData(String data) {

    }

    @Override
    public void setFailure() {

    }

    @OnClick({R.id.bt_compare, R.id.iv_eyes1, R.id.iv_eyes2,R.id.tv_read})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_compare:
                setCode();
                break;
            case R.id.iv_eyes1:
                if (iv_eyes1.isSelected()) {
                    iv_eyes1.setSelected(false);
                    iv_eyes1.setImageResource(R.drawable.icon_hide);
                    EdittextUtil.setHiden(false, et_account_password);
                } else {
                    iv_eyes1.setSelected(true);
                    iv_eyes1.setImageResource(R.drawable.icon_public);
                    EdittextUtil.setHiden(true, et_account_password);
                }
                break;
            case R.id.iv_eyes2:
                if (iv_eyes2.isSelected()) {
                    iv_eyes2.setSelected(false);
                    iv_eyes2.setImageResource(R.drawable.icon_hide);
                    EdittextUtil.setHiden(false, et_account_confirm);
                } else {
                    iv_eyes2.setSelected(true);
                    iv_eyes2.setImageResource(R.drawable.icon_public);
                    EdittextUtil.setHiden(true, et_account_confirm);
                }

                break;
            case R.id.tv_read:

                Intent intent1 = new Intent(this, CustomWebActivity.class);
                intent1.putExtra("url", APIConstant.H5_userAgreement);
                intent1.putExtra("title", getResources().getString(R.string.text_user_agreement));
                intent1.putExtra("isHideTitle", false);
                startActivity(intent1);

                break;
            default:
                break;
        }
    }

}
